#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010 Boštjan Seničar
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pango
#import pygtk
import gtk

class mluc_view:
	
	def __init__(self,parent,row):
		self.parent = parent
		self.row = row
		
		# get the tree selection
		selection = self.parent.tree.get_selection()
		
		#from tree selection get treemodel and treeiter
		treemodel, treeiter = selection.get_selected()
		signature = treemodel.get_value(treeiter,2)
		
		data = self.parent.profile.tag_data(self.row)

		self.mluc(data)
		
		if signature == 'desc':
			self.parent.frame_label.set_text("<b>Profile Description Tag</b>")
			self.parent.frame_label.set_use_markup(True)
		elif signature == 'cprt':
			self.parent.frame_label.set_text("<b>Copyright Tag</b>")
			self.parent.frame_label.set_use_markup(True)
	
	def mluc(self,data=None):
		
		if(data[:4] == 'mluc'):
			
			child = self.parent.wTree.get_object('mluc')
			mluctext = self.parent.wTree.get_object("mluctext")
			hex_buffer = self.parent.wTree.get_object("hex_buffer")	
			
			number_of_names = self.parent.wTree.get_object("number_of_names")
			name_record_size = self.parent.wTree.get_object("name_record_size")
			
			lang_code = self.parent.wTree.get_object("lang_code")
			country_code = self.parent.wTree.get_object("country_code")
			lenght_label = self.parent.wTree.get_object("lenght")
			offset_label = self.parent.wTree.get_object("offset")
									
			num, size, lang, country, lenght, offset, text = self.parent.profile.multiLocalizedUnicodeType(data)
			
			text, extra_chars = self.parent.bytes_to_hexview(text)

			number_of_names.set_text('%s' % num)
			
			name_record_size.set_text('%s' % size)
			
			lang_code.set_text(lang)
			
			country_code.set_text(country)
			
			lenght_label.set_text('%s / %s' % (lenght, hex(lenght)))		
			offset_label.set_text('%s / %s' % (offset, hex(offset)))
			
			hex_buffer.set_text(text[:-extra_chars]);

			font = pango.FontDescription("monospace")
			mluctext.modify_font(font)
			mluctext.set_wrap_mode(gtk.WRAP_NONE)
			
			self.parent.replace_child(child)
		else:
			text = self.parent.profile.multiLocalizedUnicodeType(data)
			text = text.replace('\x00','.')
			child = gtk.Label()
			child.set_text(str(text))
			child.set_use_markup(True)
			child.set_line_wrap(True)
			self.parent.replace_child(child)
			